<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>21.19. SimpleHTTPServer — Simple HTTP request handler &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="21. Internet Protocols and Support" href="internet.html" />
    <link rel="next" title="21.20. CGIHTTPServer — CGI-capable HTTP request handler" href="cgihttpserver.html" />
    <link rel="prev" title="21.18. BaseHTTPServer — Basic HTTP server" href="basehttpserver.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="cgihttpserver.html" title="21.20. CGIHTTPServer — CGI-capable HTTP request handler"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="basehttpserver.html" title="21.18. BaseHTTPServer — Basic HTTP server"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-SimpleHTTPServer">
<h1>21.19. <tt class="xref docutils literal"><span class="pre">SimpleHTTPServer</span></tt> &#8212; Simple HTTP request handler<a class="headerlink" href="#module-SimpleHTTPServer" title="Permalink to this headline">¶</a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="xref docutils literal"><span class="pre">SimpleHTTPServer</span></tt> module has been merged into <tt class="xref docutils literal"><span class="pre">http.server</span></tt> in
Python 3.0.  The <a class="reference external" href="../glossary.html#term-to3"><em class="xref">2to3</em></a> tool will automatically adapt imports when
converting your sources to 3.0.</p>
</div>
<p>The <tt class="xref docutils literal"><span class="pre">SimpleHTTPServer</span></tt> module defines a single class,
<a title="SimpleHTTPServer.SimpleHTTPRequestHandler" class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a>, which is interface-compatible with
<a title="BaseHTTPServer.BaseHTTPRequestHandler" class="reference external" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler"><tt class="xref docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></tt></a>.</p>
<p>The <tt class="xref docutils literal"><span class="pre">SimpleHTTPServer</span></tt> module defines the following class:</p>
<dl class="class">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler">
<em class="property">
class </em><tt class="descclassname">SimpleHTTPServer.</tt><tt class="descname">SimpleHTTPRequestHandler</tt><big>(</big><em>request</em>, <em>client_address</em>, <em>server</em><big>)</big><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>This class serves files from the current directory and below, directly
mapping the directory structure to HTTP requests.</p>
<p>A lot of the work, such as parsing the request, is done by the base class
<a title="BaseHTTPServer.BaseHTTPRequestHandler" class="reference external" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler"><tt class="xref docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></tt></a>.  This class implements the
<a title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"><tt class="xref docutils literal"><span class="pre">do_GET()</span></tt></a> and <a title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD"><tt class="xref docutils literal"><span class="pre">do_HEAD()</span></tt></a> functions.</p>
<p>The following are defined as class-level attributes of
<a title="SimpleHTTPServer.SimpleHTTPRequestHandler" class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a>:</p>
<dl class="attribute">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.server_version">
<tt class="descname">server_version</tt><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.server_version" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This will be <tt class="docutils literal"><span class="pre">&quot;SimpleHTTP/&quot;</span> <span class="pre">+</span> <span class="pre">__version__</span></tt>, where <tt class="docutils literal"><span class="pre">__version__</span></tt> is
defined at the module level.</p>
<dl class="attribute">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map">
<tt class="descname">extensions_map</tt><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map" title="Permalink to this definition">¶</a></dt>
<dd>A dictionary mapping suffixes into MIME types. The default is
signified by an empty string, and is considered to be
<tt class="docutils literal"><span class="pre">application/octet-stream</span></tt>. The mapping is used case-insensitively,
and so should contain only lower-cased keys.</dd></dl>

<p>The <a title="SimpleHTTPServer.SimpleHTTPRequestHandler" class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a> class defines the following methods:</p>
<dl class="method">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD">
<tt class="descname">do_HEAD</tt><big>(</big><big>)</big><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" title="Permalink to this definition">¶</a></dt>
<dd>This method serves the <tt class="docutils literal"><span class="pre">'HEAD'</span></tt> request type: it sends the headers it
would send for the equivalent <tt class="docutils literal"><span class="pre">GET</span></tt> request. See the <a title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"><tt class="xref docutils literal"><span class="pre">do_GET()</span></tt></a>
method for a more complete explanation of the possible headers.</dd></dl>

<dl class="method">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET">
<tt class="descname">do_GET</tt><big>(</big><big>)</big><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="Permalink to this definition">¶</a></dt>
<dd><p>The request is mapped to a local file by interpreting the request as a
path relative to the current working directory.</p>
<p>If the request was mapped to a directory, the directory is checked for a
file named <tt class="docutils literal"><span class="pre">index.html</span></tt> or <tt class="docutils literal"><span class="pre">index.htm</span></tt> (in that order). If found, the
file&#8217;s contents are returned; otherwise a directory listing is generated
by calling the <tt class="xref docutils literal"><span class="pre">list_directory()</span></tt> method. This method uses
<a title="os.listdir" class="reference external" href="os.html#os.listdir"><tt class="xref docutils literal"><span class="pre">os.listdir()</span></tt></a> to scan the directory, and returns a <tt class="docutils literal"><span class="pre">404</span></tt> error
response if the <tt class="xref docutils literal"><span class="pre">listdir()</span></tt> fails.</p>
<p>If the request was mapped to a file, it is opened and the contents are
returned.  Any <a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> exception in opening the requested file is
mapped to a <tt class="docutils literal"><span class="pre">404</span></tt>, <tt class="docutils literal"><span class="pre">'File</span> <span class="pre">not</span> <span class="pre">found'</span></tt> error. Otherwise, the content
type is guessed by calling the <tt class="xref docutils literal"><span class="pre">guess_type()</span></tt> method, which in turn
uses the <em>extensions_map</em> variable.</p>
<p>A <tt class="docutils literal"><span class="pre">'Content-type:'</span></tt> header with the guessed content type is output,
followed by a <tt class="docutils literal"><span class="pre">'Content-Length:'</span></tt> header with the file&#8217;s size and a
<tt class="docutils literal"><span class="pre">'Last-Modified:'</span></tt> header with the file&#8217;s modification time.</p>
<p>Then follows a blank line signifying the end of the headers, and then the
contents of the file are output. If the file&#8217;s MIME type starts with
<tt class="docutils literal"><span class="pre">text/</span></tt> the file is opened in text mode; otherwise binary mode is used.</p>
<p>For example usage, see the implementation of the <tt class="xref docutils literal"><span class="pre">test()</span></tt> function.</p>
<p>
<span class="versionmodified">New in version 2.5: </span>The <tt class="docutils literal"><span class="pre">'Last-Modified'</span></tt> header.</p>
</dd></dl>

</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer)." class="reference external" href="basehttpserver.html#module-BaseHTTPServer"><tt class="xref docutils literal"><span class="pre">BaseHTTPServer</span></tt></a></dt>
<dd>Base class implementation for Web server and request handler.</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="basehttpserver.html"
                                  title="previous chapter">21.18. <tt class="docutils literal docutils literal"><span class="pre">BaseHTTPServer</span></tt> &#8212; Basic HTTP server</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="cgihttpserver.html"
                                  title="next chapter">21.20. <tt class="docutils literal docutils literal docutils literal"><span class="pre">CGIHTTPServer</span></tt> &#8212; CGI-capable HTTP request handler</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/simplehttpserver.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="cgihttpserver.html" title="21.20. CGIHTTPServer — CGI-capable HTTP request handler"
             >next</a> |</li>
        <li class="right" >
          <a href="basehttpserver.html" title="21.18. BaseHTTPServer — Basic HTTP server"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="internet.html" >21. Internet Protocols and Support</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>